package com.company.lagou.homework.homework1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;

public class DouDizhu {

        public static void main(String[] args) {
            //创建一个HashMap集合,牌值对应大小
            HashMap<Integer,String> hashmap = new HashMap<Integer,String>();

            //存储牌的编号
            ArrayList<Integer> arraylist = new ArrayList<Integer>();

            //创建牌的大小数组
            int index = 0;
            //放入大小王
            hashmap.put(index,"小王");//存入小王
            arraylist.add(index);
            index++;
            hashmap.put(index,"大王");//存入大王
            arraylist.add(index);
            index++;

            String[] numbers={"2","2","2","2","A","A","A","A","K","K","K","K",
                    "J","J","J","J","10","10","10","10","9","9","9","9","8","8","8","8",
                    "7","7","7","7","6","6","6","6","3","3","3","3","5","5","5","5",
                    "4","4","4","4","3","3","3","3",};

            //索引从0开始向hashmap集合中存储牌的编号的大小，向arraylist集合中存储牌的编号

            for(String number : numbers) {
                    String poker = number;
                    hashmap.put(index,poker);
                    arraylist.add(index);//将每张牌的编号同样存储在arraylist集合中
                    index++;//每存储一张牌，编号加1

            }

		   //洗牌
            Collections.shuffle(arraylist);

            //发牌
            TreeSet<Integer> player1 = new TreeSet<Integer>();//玩家1
            TreeSet<Integer> player2 = new TreeSet<Integer>();//玩家2
            TreeSet<Integer> player3 = new TreeSet<Integer>();//玩家3
            TreeSet<Integer> basepoker = new TreeSet<Integer>();//底牌

            for(int i = 0; i < arraylist.size(); i++) {
                if(i<51) {//前51张牌按顺序依次发给3位玩家
                    if(i%3==0) {
                        //这里通过ArrayList集合中的get(index)方法获取索引i对应的牌的编号
                        player1.add(arraylist.get(i));
                    }else if(i%3==1) {
                        player2.add(arraylist.get(i));
                    }else {
                        player3.add(arraylist.get(i));
                    }
                }else {
                    basepoker.add(arraylist.get(i));
                }
            }

            //通过遍历TreeSet集合，获取牌的编号
            DouDizhu.licensing("player1：",player1,hashmap);
            DouDizhu.licensing("player2：",player2,hashmap);
            DouDizhu.licensing("player3：",player3,hashmap);
            DouDizhu.licensing("basepoker：",basepoker,hashmap);
        }
        public static void licensing(String name,TreeSet<Integer> ts,HashMap<Integer,String> hashmap) {
            System.out.print(name);
            int num=0;
            for(Integer key : ts) {//遍历形参中的每一个TreeSet集合
                //通过HashMap集合中的get(key)方法获取键对应的值（也就是牌的编号对应牌的花色大小）
                String value = hashmap.get(key);
                //这里的if-else是控制一下输出格式，保证最后一张牌的后面没有多余的逗号
                if(num < ts.size()-1) {
                    System.out.print(value + " , ");
                }else {
                    System.out.print(value);
                }
                num++;
            }
            System.out.println(" ");

        }
    }

